package com.example.wan1.view.home;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.base.fragment.BaseFragment;
import com.example.wan1.bus.RxBus;
import com.example.wan1.constant.Constants;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.web.JumpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import dagger.android.support.AndroidSupportInjection;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/28 14:11
 * @Description: 包含最新文章、最新项目
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_tab)
    TabLayout mHomeTab;
    @BindView(R.id.home_vp)
    ViewPager mHomeVp;
    @BindView(R.id.search)
    ImageView mSearch;

    @Inject
    List<String> mTitles;
    @Inject
    List<Fragment> mFragments;
    @Inject
    SubPagerAdapter mAdapter;

    private void loadData() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidSupportInjection.inject(this);
        super.onCreate(savedInstanceState);
        //mTitles = new ArrayList<>();
        //mFragments = new ArrayList<>();
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initData() {
        mTitles.clear();
        mFragments.clear();

        mTitles.add(getString(R.string.home_new_article_title));
        mTitles.add(getString(R.string.home_new_project_title));

        mFragments.add(new ArticleFragment().newInstance(""));
        mFragments.add(new PerProjectFragment().newInstance(Constants.PROJECT_NEWEST_TYPE, 0));
        loadData();
    }

    @Override
    protected void initView() {
        //mAdapter = new SubPagerAdapter(getFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        //    java.lang.IllegalStateException: FragmentManager is already executing transactions
        //mAdapter = new SubPagerAdapter(getChildFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mAdapter.setData(mFragments, mTitles);
        mHomeVp.setAdapter(mAdapter);
        mHomeTab.setupWithViewPager(mHomeVp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mHomeTab.setElevation(0);
        }
        mHomeTab.removeAllTabs();
        for (int i = 0; i < mTitles.size(); i++) {
            mHomeTab.addTab(mHomeTab.newTab().setText(mTitles.get(i)));
        }

        mSearch.setOnClickListener(v -> JumpTool.goSearch(mContext, Constants.SEARCH_NORMAL_TYPE));
    }

    @BindView(R.id.home_tbar)
    Toolbar mToolbar;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RxBus.getDefault().subscribe(mContext, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                if (s.equals("theme_color")) {
                    int color = ToolsUtils.getThemeColor(getActivity());
                    mHomeTab.setSelectedTabIndicatorColor(color);
                    mHomeTab.setTabTextColors(Color.parseColor("#333333"), color);
                    mToolbar.setBackgroundColor(color);
                }
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            mHomeTab.setSelectedTabIndicatorColor(color);
//            mHomeTab.setTabTextColors(ColorStateList.valueOf(color));
            mHomeTab.setTabTextColors(Color.parseColor("#333333"), color);
            mToolbar.setBackgroundColor(color);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus.getDefault().unregister(mContext);
    }
}